package filescanner;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class HanderXml {
	private static DocumentBuilderFactory dbf = DocumentBuilderFactory
			.newInstance();
	private static DocumentBuilder db;
	public static Map musicMap = null;
	public Map newMusicMap = null;
	public static HanderXml handerXml;

	public static Map getMusic() {
		try {
			db = dbf.newDocumentBuilder();
			File file = new File("musicList.xml");
			Document doc;
			doc = db.parse(file);
			NodeList nl = doc.getElementsByTagName("music");
			int len = nl.getLength();
			musicMap = new HashMap();
			for (int i = 0; i < len; i++) {

				Element tempMusic = (Element) nl.item(i);
				Node eltName = tempMusic.getElementsByTagName("name").item(0);
				Node eltSize = tempMusic.getElementsByTagName("size").item(0);

				String name = eltName.getFirstChild().getNodeValue();
				String size = eltSize.getFirstChild().getNodeValue();
				musicMap.put(name, size);
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();

		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return musicMap;

	}

	public static void saveMusic(String newName, String newSize) {

		try {
			db = dbf.newDocumentBuilder();
			File file = new File("musicList.xml");
			Document doc;
			doc = db.parse(file);
			NodeList root = doc.getElementsByTagName("musics");
			Element music = doc.createElement("music");
			music.setAttribute("sn", "3");
			Element name = doc.createElement("name");
			Element size = doc.createElement("size");
			Text tName = doc.createTextNode(newName);
			name.appendChild(tName);
			Text tSize = doc.createTextNode(newSize);
			size.appendChild(tSize);
			music.appendChild(name);
			music.appendChild(size);
			root.item(0).appendChild(music);
			FileOutputStream outStream = new FileOutputStream("musicList.xml");
			OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
			((XmlDocument) doc).write(outWriter, "GB2312");
			outWriter.close();
			outStream.close();

		} catch (ParserConfigurationException e) {
			e.printStackTrace();

		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void deleteMusic(String name) {

		try {
			db = dbf.newDocumentBuilder();
			File file = new File("musicList.xml");
			Document doc;
			doc = db.parse(file);
			NodeList nl = doc.getElementsByTagName("music");

			for (int i = 0; i < nl.getLength(); i++) {
				Element tempMusic = (Element) nl.item(i);
				Node eltName = tempMusic.getElementsByTagName("name").item(0);
				String oldName = eltName.getFirstChild().getNodeValue();

				if (oldName.toString().trim().equals(name.toString().trim())) {
					tempMusic.getParentNode().removeChild(tempMusic);
				}
			}
			FileOutputStream outStream = new FileOutputStream("musicList.xml");
			OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
			((XmlDocument) doc).write(outWriter, "gb2312");
			outWriter.close();
			outStream.close();

		} catch (ParserConfigurationException e) {
			e.printStackTrace();

		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {

		handerXml = new HanderXml();
		handerXml.deleteMusic("20100121.3gp");

	}
}
